package com.hs.service.impl;
/*
 * @Author:iWeeper
 * @Date:2022/6/21 12:47
 * @Version:1.0
 * */

import com.hs.entity.TbRole;
import com.hs.mapper.TbMenuMapper;
import com.hs.mapper.TbRoleMapper;
import com.hs.service.RoleService;
import com.hs.utils.DruidUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private TbRoleMapper roleMapper;

    @Autowired
    private TbMenuMapper menuMapper;

    @Override
    public List<Map<String, Object>> queryAll() {
        return roleMapper.queryAll();
    }

    @Override
    public Boolean saveMenus(String menuIds,Integer roleId) throws Exception{
        boolean result = false;
        //判断参数合法性
        if(StringUtils.isNotBlank(menuIds)&&roleId!=null){
            //将menuIds拆分为数组
            String[] menuArray = menuIds.split(",");
            //封装sql批处理参数
            Object[][] params = new Object[menuArray.length][2];
            for(int i=0;i<menuArray.length;i++){
                params[i][0]=roleId;
                params[i][1]=menuArray[i];
            }
            roleMapper.deleteByRoleId(roleId);
            //保存新的菜单权限
            roleMapper.insertBatch(params);
            //以上代码都没有异常则返回true;
            result = true;
        }
        return result;
    }
}
